#coding=utf-8

"""
本程序为用户或者银行管理员入口,其中c=1代表银行管理员,c=2代表普通用户
"""
import pickle
import sys
import os
import socket
import Conf
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from lib import *
from lib.modules import *

def login(username,pwd):
    """
    登录
    :param username: 用户名
    :param pwd:
    :return:
    """
    obj.sendall(bytes(username+','+pwd, encoding='utf-8'))
    ret = obj.recv(1024)
    r = str(ret, encoding='utf-8')
    if r=='y':
        return 1
    else:
        return 0


def regist(username, pwd,x):
    """
    注册功能
    :param username:
    :param pwd:
    :return:
    """
    obj.sendall(bytes(username+','+pwd, encoding='utf-8'))
    ret = obj.recv(1024)
    if r =='y':
        return 1
    else:
        return 0


def user_identify_authentication(username,pwd,x):
    """

    :param username:
    :param pwd:
    :param x:
    :return:
    """
    a = raw_input('请选择: 1. 登录  2.注册')
    obj.sendall(bytes(a,encoding='utf-8'))
    obj.recv(1024)
    if a =='1':
        ret = login(username,pwd)
        if ret:
            print('登录成功')
            return 1
        else:
            print('登录失败')
            return 0
    elif a == '2':
        ret = regist(usrname, pwd, x)
        if ret:
            print('注册成功')
            return 1
        else:
            print('用户名已存在或银行管理员拒绝')
            return 0


def main(x):
    usrname = input('请输入用户名')
    pwd = input('请输入密码')
    if user_identity_authentication(usrname, pwd, x):  # 如果验证身份成功
        if x == '1':  # 处理用户注册信息

            while True:
                s = pickle.load(open(settings.A, 'rb'))
                if s[1] == 0:
                    time.sleep(30)
                    continue
                elif s[1] == 1:
                    while True:
                        a = input('用户请求注册，输入1同意，2拒绝')
                        if a == '1':
                            s = [1, 0]
                            pickle.dump(s, open(settings.A, 'wb'))
                            break
                        elif a == '2':
                            s = [2, 0]
                            pickle.dump(s, open(settings.A, 'wb'))
                            break
                        else:
                            print('输入有误')
                    break
        else:  # 普通用户登陆后
            interactive()  # 进行交互


def interactive():
    while True:
        a=input('请选择 1.还款 2.取现 3.转账 4.消费 5.存钱  q退出')
        obj.sendall(bytes(a,encoding='utf-8'))
        r=obj.recv(1024) #接收账户信息
        ret=str(r,encoding='utf-8')
        print(ret)
        if a !='4'and a !='q':
            b=input('请输入金额')
            obj.sendall(bytes(b,encoding='utf-8'))
        elif a=='q':
            break


if __name__ =='__main__':
    obj = socket.socket()
    obj.connect(('127.0.0.1',8888))
    while True:
        x = raw_input('请选择1.银行管理员 2.用户 q、退出')
        obj.sendall(bytes(x,encoding='utf-8'))
        obj.recv(1024)    #确认收到用户类别
        if x=='1' or x=='2':
            main(x)
            break
        elif x=='q':
            break
        else:
            print('输入有误请重新输入')
            
    obj.close()


